<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ToolAction\CreateInvitationCodeActions;
use App\Admin\Repositories\InvitationCode;
use App\Models\Member;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Tools;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class InvitationCodeController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new InvitationCode(), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->export(['id' => 'ID', 'code' => '邀请码', 'created_at' => '创建时间']);
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function ($value) {
                return Member::find($value)->username ?? '';
            });
            $grid->column('type')->display(function ($value) {
                $state = ['medical' => '病例', 'register' => '注册'];
                return isset($state[$value]) ? $state[$value] : '未使用';
            })->label()->filter(
                Grid\Column\Filter\In::make(['medical' => '病例', 'register' => '注册'])
            );
            $grid->column('code')->copyable();
            $grid->column('add_ip');
            $grid->column('status')->display(function ($value) {
                $state = ['未使用', '已使用'];
                return isset($state[$value]) ? $state[$value] : '未知';
            })->label([
                '0' => 'danger',
                '1' => 'success',
            ]);
            $grid->column('use_time')->sortable();
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('code');
                $filter->equal('type', '用途')->select(['medical' => '病例', 'register' => '注册']);
            });
            
            //  /**
            //  * 按钮控制.
            //  */
            // // @permissions
            // if (!Admin::user()->can('user-info-create')) {
            //     $grid->disableCreateButton();
            //     // $grid->disableQuickCreateButton();
            // }
            // // @permissions
            // if (!Admin::user()->can('user-info-update')) {
            //     $grid->disableEditButton();
            //     // $grid->disableQuickEditButton();
            // }
            // @permissions
            if (Admin::user()->can('invitation-code-create')) {
                  /**
                     * 工具按钮.
                     */
                $grid->tools(function (Tools $tools) {
                    $tools->append(new CreateInvitationCodeActions());
                });
            }
          
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new InvitationCode(), function (Show $show) {
            $show->field('id');
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('code');
            $show->field('add_ip');
            $show->field('status');
            $show->field('use_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new InvitationCode(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('type');
            $form->text('code');
            $form->text('add_ip');
            $form->text('status');
            $form->text('use_time');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
